/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.NotExistingObjectException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlDataTable;
import managedBeans.util.CourseToEdit;
import managedBeans.util.NodeToView;
import valueObject.CourseVO;
import valueObject.NodeVO;

/**
 *
 * @author David
 */
public class nodeMB {

    private HtmlDataTable table;    
    private String description;
    private String data;
    
    
    @EJB
    public SuiLibFacade suiLibFacade;
    /**
     * Creates a new instance of nodeMB
     */
    public nodeMB() {
    }
    
    
    public List<NodeVO> nodesByCourse() {
        CourseVO courseVO = null;
        try {
            courseVO = suiLibFacade.getCourseVOById(CourseToEdit.getInstance().getId());
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(nodeMB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return suiLibFacade.findNodesByCourse(courseVO);
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the data
     */
    public String getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(String data) {
        this.data = data;
    }
    
    public void createNode() {
        CourseVO courseVO = null;
        try {
            courseVO = suiLibFacade.getCourseVOById(CourseToEdit.getInstance().getId());
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(nodeMB.class.getName()).log(Level.SEVERE, null, ex);
        }
        NodeVO nodeVo = new NodeVO();
        nodeVo.setDescription(description);
        nodeVo.setContent(data);
        nodeVo.setCourse(courseVO);
        suiLibFacade.createNode(nodeVo);
    }
    
    public String editFormList() {
        NodeVO vo =(NodeVO) getTable().getRowData();
        NodeToView.getInstance().setId(vo.getId());
        return "nodeEdit";
    }

    
    public String viewFormList() {
        NodeVO vo =(NodeVO) getTable().getRowData();
        NodeToView.getInstance().setId(vo.getId());
        try {
            NodeVO node = suiLibFacade.findNodeById(vo.getId());
            NodeToView.getInstance().setContent(node.getContent());
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(nodeMB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "nodeHTML";
    }
    
    
    /**
     * @return the table
     */
    public HtmlDataTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(HtmlDataTable table) {
        this.table = table;
    }
    
    public void updateNode() {
        try {
            NodeVO vo = suiLibFacade.findNodeById(NodeToView.getInstance().getId());
            vo.setContent(data);
            vo.setDescription(description);
            suiLibFacade.updateNode(vo);
        } catch (NotExistingObjectException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public String viewHTML() {
        String html = NodeToView.getInstance().getContent();
        return  html;
    }
}
